home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / uucico / trans.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-20  |  36.1 KB  |  1,490 lines

  1. /* trans.c
  2.    Routines to handle file transfers.
  3.  
  4.    Copyright (C) 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char trans_rcsid[] = "$Id: trans.c,v 1.41 1995/08/17 01:25:29 ian Rel $";
  30. #endif
  31.  
  32. #include <errno.h>
  33.  
  34. #include "uudefs.h"
  35. #include "uuconf.h"
  36. #include "prot.h"
  37. #include "system.h"
  38. #include "trans.h"
  39.  
  40. /* Local functions.  */
  41.  
  42. static void utqueue P((struct stransfer **, struct stransfer *,
  43.                boolean fhead));
  44. static void utdequeue P((struct stransfer *));
  45. static void utchanalc P((struct sdaemon *qdaemon, struct stransfer *qtrans));
  46. __inline__ static struct stransfer *qtchan P((int ichan));
  47. __inline__ static void utchanfree P((struct stransfer *qtrans));
  48. static boolean fttime P((struct sdaemon *qdaemon, long *pisecs,
  49.              long *pimicros));
  50. static boolean fcheck_queue P((struct sdaemon *qdaemon));
  51. static boolean ftadd_cmd P((struct sdaemon *qdaemon, const char *z,
  52.                 size_t cdata, int iremote, boolean flast));
  53. static boolean fremote_hangup_reply P((struct stransfer *qtrans,
  54.                        struct sdaemon *qdaemon));
  55. static boolean flocal_poll_file P((struct stransfer *qtrans,
  56.                    struct sdaemon *qdaemon));
  57.  
  58. /* Queue of transfer structures that are ready to start which have
  59.    been requested by the local system.  These are only permitted to
  60.    start when the local system is the master.  */
  61. static struct stransfer *qTlocal;
  62.  
  63. /* Queue of transfer structures that are ready to start which have
  64.    been requested by the remote system.  These are responses to
  65.    commands received from the remote system, and should be started as
  66.    soon as possible.  */
  67. static struct stransfer *qTremote;
  68.  
  69. /* Queue of transfer structures that have been started and want to
  70.    send information.  This should be static, but the 'a' protocol
  71.    looks at it, at least for now.  */
  72. struct stransfer *qTsend;
  73.  
  74. /* Queue of transfer structures that have been started and are waiting
  75.    to receive information.  */
  76. static struct stransfer *qTreceive;
  77.  
  78. /* Queue of free transfer structures.  */
  79. static struct stransfer *qTavail;
  80.  
  81. /* Array of transfer structures indexed by local channel number.  This
  82.    is maintained for local jobs.  */
  83. static struct stransfer *aqTchan[IMAX_CHAN + 1];
  84.  
  85. /* Number of local channel numbers currently allocated.  */
  86. static int cTchans;
  87.  
  88. /* Next channel number to allocate.  */
  89. static int iTchan;
  90.  
  91. /* Array of transfer structures indexed by remote channel number.
  92.    This is maintained for remote jobs.  */
  93. static struct stransfer *aqTremote[IMAX_CHAN + 1];
  94.  
  95. /* The transaction we are currently receiving.  This is used to avoid
  96.    getting the time too frequently.  */
  97. static struct stransfer *qTtiming_rec;
  98.  
  99. /* The time from which to charge any received data.  This is either
  100.    the last time we charged for received data, or the last time
  101.    something was put on the empty receive queue.  */
  102. static long iTrecsecs;
  103. static long iTrecmicros;
  104.  
  105. /* The minimum amount of time, in seconds, to wait between times we
  106.    check the spool directory, if we are busy transferring data.  If we
  107.    have nothing to do, we will check the spool directory regardless of
  108.    how long ago the last check was.  This should probably be
  109.    configurable.  */
  110. #define CCHECKWAIT (600)
  111.  
  112. /* The time we last checked the spool directory for work.  This is set
  113.    from the return value of ixsysdep_process_time, not ixsysdep_time,
  114.    for convenience in the routines which use it.  */
  115. static long iTchecktime;
  116.  
  117. /* The size of the command we have read so far in ftadd_cmd.  */
  118. static size_t cTcmdlen;
  119.  
  120. /* The structure we use when waiting for an acknowledgement of a
  121.    confirmed received file in fsent_receive_ack, and a list of those
  122.    structures.  */
  123.  
  124. struct sreceive_ack
  125. {
  126.   struct sreceive_ack *qnext;
  127.   char *zto;
  128.   char *ztemp;
  129.   boolean fmarked;
  130. };
  131.  
  132. static struct sreceive_ack *qTreceive_ack;
  133.  
  134. /* Queue up a transfer structure before *pq.  This puts it at the head
  135.    or the tail of the list headed by *pq.  */
  136.  
  137. static void
  138. utqueue (pq, q, fhead)
  139.      struct stransfer **pq;
  140.      struct stransfer *q;
  141.      boolean fhead;
  142. {
  143.   if (*pq == NULL)
  144.     {
  145.       *pq = q;
  146.       q->qprev = q->qnext = q;
  147.     }
  148.   else
  149.     {
  150.       q->qnext = *pq;
  151.       q->qprev = (*pq)->qprev;
  152.       q->qprev->qnext = q;
  153.       q->qnext->qprev = q;
  154.       if (fhead)
  155.     *pq = q;
  156.     }
  157.   q->pqqueue = pq;
  158. }
  159.  
  160. /* Dequeue a transfer structure.  */
  161.  
  162. static void
  163. utdequeue (q)
  164.      struct stransfer *q;
  165. {
  166.   if (q->pqqueue != NULL)
  167.     {
  168.       if (*(q->pqqueue) == q)
  169.     {
  170.       if (q->qnext == q)
  171.         *(q->pqqueue) = NULL;
  172.       else
  173.         *(q->pqqueue) = q->qnext;
  174.     }
  175.       q->pqqueue = NULL;
  176.     }
  177.   if (q->qprev != NULL)
  178.     q->qprev->qnext = q->qnext;
  179.   if (q->qnext != NULL)
  180.     q->qnext->qprev = q->qprev;
  181.   q->qprev = NULL;
  182.   q->qnext = NULL;
  183. }
  184.  
  185. /* Queue up a transfer structure requested by the local system.  */
  186.  
  187. /*ARGSIGNORED*/
  188. boolean
  189. fqueue_local (qdaemon, qtrans)
  190.      struct sdaemon *qdaemon;
  191.      struct stransfer *qtrans;
  192. {
  193.   utdequeue (qtrans);
  194.   utqueue (&qTlocal, qtrans, FALSE);
  195.   return TRUE;
  196. }
  197.  
  198. /* Queue up a transfer structure requested by the remote system.  The
  199.    stransfer structure should have the iremote field set.  We need to
  200.    record it, so that any subsequent data associated with this
  201.    channel can be routed to the right place.  */
  202.  
  203. boolean
  204. fqueue_remote (qdaemon, qtrans)
  205.      struct sdaemon *qdaemon;
  206.      struct stransfer *qtrans;
  207. {
  208.   DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO, "fqueue_remote: Channel %d",
  209.           qtrans->iremote);
  210.   if (qtrans->iremote > 0)
  211.     aqTremote[qtrans->iremote] = qtrans;
  212.   utdequeue (qtrans);
  213.   utqueue (&qTremote, qtrans, FALSE);
  214.   return TRUE;
  215. }
  216.  
  217. /* Queue up a transfer with something to send.  */
  218.  
  219. boolean
  220. fqueue_send (qdaemon, qtrans)
  221.      struct sdaemon *qdaemon;
  222.      struct stransfer *qtrans;
  223. {
  224. #if DEBUG > 0
  225.   if (qtrans->psendfn == NULL)
  226.     ulog (LOG_FATAL, "fqueue_send: Bad call");
  227. #endif
  228.   utdequeue (qtrans);
  229.  
  230.   /* Sort the send queue to always send commands before files, and to
  231.      sort jobs by grade.  */
  232.   if (qTsend == NULL)
  233.     utqueue (&qTsend, qtrans, FALSE);
  234.   else
  235.     {
  236.       register struct stransfer *q;
  237.       boolean ffirst;
  238.  
  239.       ffirst = TRUE;
  240.       q = qTsend;
  241.       do
  242.     {
  243.       if (! qtrans->fsendfile && q->fsendfile)
  244.         break;
  245.       if ((! qtrans->fsendfile || q->fsendfile)
  246.           && UUCONF_GRADE_CMP (qtrans->s.bgrade, q->s.bgrade) < 0)
  247.         break;
  248.  
  249.       ffirst = FALSE;
  250.       q = q->qnext;
  251.     }
  252.       while (q != qTsend);
  253.  
  254.       qtrans->qnext = q;
  255.       qtrans->qprev = q->qprev;
  256.       q->qprev = qtrans;
  257.       qtrans->qprev->qnext = qtrans;
  258.       if (ffirst)
  259.     qTsend = qtrans;
  260.       qtrans->pqqueue = &qTsend;
  261.     }
  262.  
  263.   return TRUE;
  264. }
  265.  
  266. /* Queue up a transfer with something to receive.  */
  267.  
  268. boolean
  269. fqueue_receive (qdaemon, qtrans)
  270.      struct sdaemon *qdaemon;
  271.      struct stransfer *qtrans;
  272. {
  273. #if DEBUG > 0
  274.   if (qtrans->precfn == NULL)
  275.     ulog (LOG_FATAL, "fqueue_receive: Bad call");
  276. #endif
  277.  
  278.   /* If this is the only item on the receive queue, we do not want to
  279.      charge it for any time during which we have not been waiting for
  280.      anything, so update the receive timestamp.  */
  281.   if (qTreceive == NULL)
  282.     iTrecsecs = ixsysdep_process_time (&iTrecmicros);
  283.  
  284.   utdequeue (qtrans);
  285.   utqueue (&qTreceive, qtrans, FALSE);
  286.  
  287.   return TRUE;
  288. }
  289.  
  290. /* Get a new local channel number.  */
  291.  
  292. static void
  293. utchanalc (qdaemon, qtrans)
  294.      struct sdaemon *qdaemon;
  295.      struct stransfer *qtrans;
  296. {
  297.   do
  298.     {
  299.       ++iTchan;
  300.       if (iTchan > qdaemon->cchans)
  301.     iTchan = 1;
  302.     }
  303.   while (aqTchan[iTchan] != NULL);
  304.  
  305.   qtrans->ilocal = iTchan;
  306.   aqTchan[iTchan] = qtrans;
  307.   ++cTchans;
  308. }
  309.  
  310. /* Return the transfer for a channel number.  */
  311.  
  312. __inline__
  313. static struct stransfer *
  314. qtchan (ic)
  315.      int ic;
  316. {
  317.   return aqTchan[ic];
  318. }
  319.  
  320. /* Clear the channel number for a transfer.  */
  321.  
  322. __inline__
  323. static void
  324. utchanfree (qt)
  325.      struct stransfer *qt;
  326. {
  327.   if (qt->ilocal != 0)
  328.     {
  329.       aqTchan[qt->ilocal] = NULL;
  330.       qt->ilocal = 0;
  331.       --cTchans;
  332.     }
  333. }
  334.  
  335. /* Allocate a new transfer structure.  */
  336.  
  337. struct stransfer *
  338. qtransalc (qcmd)
  339.      struct scmd *qcmd;
  340. {
  341.   register struct stransfer *q;
  342.  
  343.   q = qTavail;
  344.   if (q != NULL)
  345.     utdequeue (q);
  346.   else
  347.     q = (struct stransfer *) xmalloc (sizeof (struct stransfer));
  348.   q->qnext = NULL;
  349.   q->qprev = NULL;
  350.   q->pqqueue = NULL;
  351.   q->psendfn = NULL;
  352.   q->precfn = NULL;
  353.   q->pinfo = NULL;
  354.   q->fsendfile = FALSE;
  355.   q->frecfile = FALSE;
  356.   q->e = EFILECLOSED;
  357.   q->ipos = 0;
  358.   q->fcmd = FALSE;
  359.   q->zcmd = NULL;
  360.   q->ccmd = 0;
  361.   q->ilocal = 0;
  362.   q->iremote = 0;
  363.   if (qcmd != NULL)
  364.     {
  365.       q->s = *qcmd;
  366.       q->s.zfrom = zbufcpy (qcmd->zfrom);
  367.       q->s.zto = zbufcpy (qcmd->zto);
  368.       q->s.zuser = zbufcpy (qcmd->zuser);
  369.       q->s.zoptions = zbufcpy (qcmd->zoptions);
  370.       q->s.ztemp = zbufcpy (qcmd->ztemp);
  371.       q->s.znotify = zbufcpy (qcmd->znotify);
  372.       q->s.zcmd = zbufcpy (qcmd->zcmd);
  373.     }
  374.   else
  375.     {
  376.       q->s.zfrom = NULL;
  377.       q->s.zto = NULL;
  378.       q->s.zuser = NULL;
  379.       q->s.zoptions = NULL;
  380.       q->s.ztemp = NULL;
  381.       q->s.znotify = NULL;
  382.       q->s.zcmd = NULL;
  383.     }
  384.   q->zlog = NULL;
  385.   q->isecs = 0;
  386.   q->imicros = 0;
  387.   q->cbytes = 0;
  388.  
  389.   return q;
  390. }
  391.  
  392. /* Free a transfer structure.  This does not free any pinfo
  393.    information that may have been allocated.  */
  394.  
  395. void
  396. utransfree (q)
  397.      struct stransfer *q;
  398. {
  399.   ubuffree (q->zcmd);
  400.   ubuffree ((char *) q->s.zfrom);
  401.   ubuffree ((char *) q->s.zto);
  402.   ubuffree ((char *) q->s.zuser);
  403.   ubuffree ((char *) q->s.zoptions);
  404.   ubuffree ((char *) q->s.ztemp);
  405.   ubuffree ((char *) q->s.znotify);
  406.   ubuffree ((char *) q->s.zcmd);
  407.   
  408.   utchanfree (q);    
  409.   if (q->iremote > 0)
  410.     {
  411.       aqTremote[q->iremote] = NULL;
  412.       q->iremote = 0;
  413.     }
  414.  
  415. #if DEBUG > 0
  416.   q->e = EFILECLOSED;
  417.   q->zcmd = NULL;
  418.   q->s.zfrom = NULL;
  419.   q->s.zto = NULL;
  420.   q->s.zuser = NULL;
  421.   q->s.zoptions = NULL;
  422.   q->s.ztemp = NULL;
  423.   q->s.znotify = NULL;
  424.   q->s.zcmd = NULL;
  425.   q->psendfn = NULL;
  426.   q->precfn = NULL;
  427. #endif
  428.  
  429.   /* Avoid any possible confusion in the timing code.  */
  430.   if (qTtiming_rec == q)
  431.     qTtiming_rec = NULL;
  432.  
  433.   utdequeue (q);
  434.   utqueue (&qTavail, q, FALSE);
  435. }
  436.  
  437. /* Get the time.  This is a wrapper around ixsysdep_process_time.  If
  438.    enough time has elapsed since the last time we got the time, check
  439.    the work queue.  */
  440.  
  441. static boolean
  442. fttime (qdaemon, pisecs, pimicros)
  443.      struct sdaemon *qdaemon;
  444.      long *pisecs;
  445.      long *pimicros;
  446. {
  447.   *pisecs = ixsysdep_process_time (pimicros);
  448.   if (*pisecs - iTchecktime >= CCHECKWAIT)
  449.     {
  450.       if (! fcheck_queue (qdaemon))
  451.     return FALSE;
  452.     }
  453.   return TRUE;
  454. }
  455.  
  456. /* Gather local commands and queue them up for later processing.  Also
  457.    recompute time based control values.  */
  458.  
  459. boolean
  460. fqueue (qdaemon, pfany)
  461.      struct sdaemon *qdaemon;
  462.      boolean *pfany;
  463. {
  464.   const struct uuconf_system *qsys;
  465.   long ival;
  466.   int bgrade;
  467.   struct uuconf_timespan *qlocal_size, *qremote_size;
  468.  
  469.   if (pfany != NULL)
  470.     *pfany = FALSE;
  471.  
  472.   qsys = qdaemon->qsys;
  473.  
  474.   /* If we are not the caller, the grade will be set during the
  475.      initial handshake, although this may be overridden by the
  476.      calledtimegrade configuration option.  */
  477.   if (! qdaemon->fcaller)
  478.     {
  479.       if (! ftimespan_match (qsys->uuconf_qcalledtimegrade, &ival,
  480.                  (int *) NULL))
  481.     bgrade = qdaemon->bgrade;
  482.       else
  483.     bgrade = (char) ival;
  484.     }
  485.   else
  486.     {
  487.       if (! ftimespan_match (qsys->uuconf_qtimegrade, &ival,
  488.                  (int *) NULL))
  489.     bgrade = '\0';
  490.       else
  491.     bgrade = (char) ival;
  492.     }
  493.  
  494.   /* Determine the maximum sizes we can send and receive.  */
  495.   if (qdaemon->fcaller)
  496.     {
  497.       qlocal_size = qsys->uuconf_qcall_local_size;
  498.       qremote_size = qsys->uuconf_qcall_remote_size;
  499.     }
  500.   else
  501.     {
  502.       qlocal_size = qsys->uuconf_qcalled_local_size;
  503.       qremote_size = qsys->uuconf_qcalled_remote_size;
  504.     }
  505.  
  506.   if (! ftimespan_match (qlocal_size, &qdaemon->clocal_size, (int *) NULL))
  507.     qdaemon->clocal_size = (long) -1;
  508.   if (! ftimespan_match (qremote_size, &qdaemon->cremote_size, (int *) NULL))
  509.     qdaemon->cremote_size = (long) -1;
  510.  
  511.   if (bgrade == '\0')
  512.     return TRUE;
  513.  
  514.   if (! fsysdep_get_work_init (qsys, bgrade))
  515.     return FALSE;
  516.  
  517.   while (TRUE)
  518.     {
  519.       struct scmd s;
  520.  
  521.       if (! fsysdep_get_work (qsys, bgrade, &s))
  522.     return FALSE;
  523.  
  524.       if (s.bcmd == 'H')
  525.     {
  526.       ulog_user ((const char *) NULL);
  527.       break;
  528.     }
  529.  
  530.       if (s.bcmd == 'P')
  531.     {
  532.       struct stransfer *qtrans;
  533.  
  534.       /* A poll file.  */
  535.       ulog_user ((const char *) NULL);
  536.       qtrans = qtransalc (&s);
  537.       qtrans->psendfn = flocal_poll_file;
  538.       if (! fqueue_local (qdaemon, qtrans))
  539.         return FALSE;
  540.       continue;
  541.     }
  542.  
  543.       ulog_user (s.zuser);
  544.  
  545.       switch (s.bcmd)
  546.     {
  547.     case 'S':
  548.     case 'E':
  549.       if (! flocal_send_file_init (qdaemon, &s))
  550.         return FALSE;
  551.       break;
  552.     case 'R':
  553.       if (! flocal_rec_file_init (qdaemon, &s))
  554.         return FALSE;
  555.       break;
  556.     case 'X':
  557.       if (! flocal_xcmd_init (qdaemon, &s))
  558.         return FALSE;
  559.       break;
  560. #if DEBUG > 0
  561.     default:
  562.       ulog (LOG_FATAL, "fqueue: Can't happen");
  563.       break;
  564. #endif
  565.     }
  566.     }      
  567.  
  568.   if (pfany != NULL)
  569.     *pfany = qTlocal != NULL;
  570.  
  571.   iTchecktime = ixsysdep_process_time ((long *) NULL);
  572.  
  573.   return TRUE;
  574. }
  575.  
  576. /* Clear everything off the work queue.  This is used when the call is
  577.    complete, or if the call is never made.  */
  578.  
  579. void
  580. uclear_queue (qdaemon)
  581.      struct sdaemon *qdaemon;
  582. {
  583.   int i;
  584.  
  585.   usysdep_get_work_free (qdaemon->qsys);
  586.  
  587.   qTlocal = NULL;
  588.   qTremote = NULL;
  589.   qTsend = NULL;
  590.   qTreceive = NULL;
  591.   cTchans = 0;
  592.   iTchan = 0;
  593.   qTtiming_rec = NULL;
  594.   cTcmdlen = 0;
  595.   qTreceive_ack = NULL;
  596.   for (i = 0; i < IMAX_CHAN + 1; i++)
  597.     {
  598.       aqTchan[i] = NULL;
  599.       aqTremote[i] = NULL;
  600.     }
  601. }
  602.  
  603. /* Recheck the work queue during a conversation.  This is only called
  604.    if it's been more than CCHECKWAIT seconds since the last time the
  605.    queue was checked.  */
  606.  
  607. static boolean
  608. fcheck_queue (qdaemon)
  609.      struct sdaemon *qdaemon;
  610. {
  611.   /* Only check if we are the master, or if there are multiple
  612.      channels, or if we aren't already trying to get the other side to
  613.      hang up.  Otherwise, there's nothing we can do with any new jobs
  614.      we might find.  */
  615.   if (qdaemon->fmaster
  616.       || qdaemon->cchans > 1
  617.       || ! qdaemon->frequest_hangup)
  618.     {
  619.       boolean fany;
  620.  
  621.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  622.               "fcheck_queue: Rechecking work queue");
  623.       if (! fqueue (qdaemon, &fany))
  624.     return FALSE;
  625.  
  626.       /* If we found something to do, and we're not the master, and we
  627.      don't have multiple channels to send new jobs over, try to
  628.      get the other side to hang up.  */
  629.       if (fany && ! qdaemon->fmaster && qdaemon->cchans <= 1)
  630.     qdaemon->frequest_hangup = TRUE;
  631.     }
  632.  
  633.   return TRUE;
  634. }
  635.  
  636. /* The main transfer loop.  The uucico daemon spends essentially all
  637.    its time in this function.  */
  638.  
  639. boolean
  640. floop (qdaemon)
  641.      struct sdaemon *qdaemon;
  642. {
  643.   boolean fret;
  644.  
  645.   fret = TRUE;
  646.  
  647.   while (! qdaemon->fhangup)
  648.     {
  649.       register struct stransfer *q;
  650.  
  651. #if DEBUG > 1
  652.       /* If we're doing any debugging, close the log and debugging
  653.      files regularly.  This will let people copy them off and
  654.      remove them while the conversation is in progresss.  */
  655.       if (iDebug != 0)
  656.     {
  657.       ulog_close ();
  658.       ustats_close ();
  659.     }
  660. #endif
  661.  
  662.       if (qdaemon->fmaster)
  663.     {
  664.       boolean fhangup;
  665.  
  666.       /* We've managed to become the master, so we no longer want
  667.          to request a hangup.  */
  668.       qdaemon->frequest_hangup = FALSE;
  669.  
  670.       fhangup = FALSE;
  671.  
  672.       if (qdaemon->fhangup_requested
  673.           && qTsend == NULL)
  674.         {
  675.           /* The remote system has requested that we transfer
  676.          control by sending CYM after receiving a file.  */
  677.           DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  678.                   "floop: Transferring control at remote request");
  679.           fhangup = TRUE;
  680.         }
  681.       else if (qTremote == NULL
  682.            && qTlocal == NULL
  683.            && qTsend == NULL
  684.            && qTreceive == NULL)
  685.         {
  686.           /* We don't have anything to do.  Try to find some new
  687.          jobs.  If we can't, transfer control.  */
  688.           if (! fqueue (qdaemon, (boolean *) NULL))
  689.         {
  690.           fret = FALSE;
  691.           break;
  692.         }
  693.           if (qTlocal == NULL)
  694.         {
  695.           DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO,
  696.                   "floop: No work for master");
  697.           fhangup = TRUE;
  698.         }
  699.         }
  700.  
  701.       if (fhangup)
  702.         {
  703.           if (! (*qdaemon->qproto->pfsendcmd) (qdaemon, "H", 0, 0))
  704.         {
  705.           fret = FALSE;
  706.           break;
  707.         }
  708.           qdaemon->fmaster = FALSE;
  709.         }
  710.     }
  711.  
  712.       /* If we are no long the master, clear any requested hangup.  We
  713.      may have already hung up before checking this variable in the
  714.      block above.  */
  715.       if (! qdaemon->fmaster)
  716.     qdaemon->fhangup_requested = FALSE;
  717.  
  718.       /* Immediately queue up any remote jobs.  We don't need local
  719.      channel numbers for them, since we can disambiguate based on
  720.      the remote channel number.  */
  721.       while (qTremote != NULL)
  722.     {
  723.       q = qTremote;
  724.       utdequeue (q);
  725.       utqueue (&qTsend, q, TRUE);
  726.     }
  727.  
  728.       /* If we are the master, or if we have multiple channels, try to
  729.      queue up additional local jobs.  */
  730.       if (qdaemon->fmaster || qdaemon->cchans > 1)
  731.     {
  732.       while (qTlocal != NULL && cTchans < qdaemon->cchans)
  733.         {
  734.           /* We have room for an additional channel.  */
  735.           q = qTlocal;
  736.           if (! fqueue_send (qdaemon, q))
  737.         {
  738.           fret = FALSE;
  739.           break;
  740.         }
  741.           utchanalc (qdaemon, q);
  742.         }
  743.       if (! fret)
  744.         break;
  745.     }
  746.  
  747.       q = qTsend;
  748.  
  749.       if (q == NULL)
  750.     {
  751.       ulog_user ((const char *) NULL);
  752.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO, "floop: Waiting for data");
  753.       if (! (*qdaemon->qproto->pfwait) (qdaemon))
  754.         {
  755.           fret = FALSE;
  756.           break;
  757.         }
  758.     }
  759.       else
  760.     {
  761.       ulog_user (q->s.zuser);
  762.  
  763.       if (! q->fsendfile)
  764.         {
  765.           /* Technically, we should add the time required for this
  766.                  call to q->isecs and q->imicros.  In practice, the
  767.                  amount of time required should be sufficiently small
  768.                  that it can be safely disregarded.  */
  769.           if (! (*q->psendfn) (q, qdaemon))
  770.         {
  771.           fret = FALSE;
  772.           break;
  773.         }
  774.         }
  775.       else
  776.         {
  777.           long isecs, imicros;
  778.           boolean fcharged;
  779.           long inextsecs = 0, inextmicros;
  780.  
  781.           if (! fttime (qdaemon, &isecs, &imicros))
  782.         {
  783.           fret = FALSE;
  784.           break;
  785.         }
  786.           fcharged = FALSE;
  787.  
  788.           if (q->zlog != NULL)
  789.         {
  790.           ulog (LOG_NORMAL, "%s", q->zlog);
  791.           ubuffree (q->zlog);
  792.           q->zlog = NULL;
  793.         }
  794.  
  795.           /* We can read the file in a tight loop until we have a
  796.          command to send, or the file send has been cancelled,
  797.          or we have a remote job to deal with.  We can
  798.          disregard any changes to qTlocal since we already
  799.          have something to send anyhow.  */
  800.           while (q == qTsend
  801.              && q->fsendfile
  802.              && qTremote == NULL)
  803.         {
  804.           char *zdata;
  805.           size_t cdata;
  806.           long ipos;
  807.  
  808.           zdata = (*qdaemon->qproto->pzgetspace) (qdaemon, &cdata);
  809.           if (zdata == NULL)
  810.             {
  811.               fret = FALSE;
  812.               break;
  813.             }
  814.  
  815.           if (ffileeof (q->e))
  816.             cdata = 0;
  817.           else
  818.             {
  819.               cdata = cfileread (q->e, zdata, cdata);
  820.               if (ffileioerror (q->e, cdata))
  821.             {
  822.               /* There is no way to report a file reading
  823.                  error, so we just drop the connection.  */
  824.               ulog (LOG_ERROR, "read: %s", strerror (errno));
  825.               fret = FALSE;
  826.               break;
  827.             }
  828.             }
  829.  
  830.           ipos = q->ipos;
  831.           q->ipos += cdata;
  832.           q->cbytes += cdata;
  833.  
  834.           if (! (*qdaemon->qproto->pfsenddata) (qdaemon, zdata,
  835.                             cdata, q->ilocal,
  836.                             q->iremote, ipos))
  837.             {
  838.               fret = FALSE;
  839.               break;
  840.             }
  841.  
  842.           if (cdata == 0)
  843.             {
  844.               /* We must update the time now, because this
  845.              call may make an entry in the statistics
  846.              file.  */
  847.               inextsecs = ixsysdep_process_time (&inextmicros);
  848.               DEBUG_MESSAGE4 (DEBUG_UUCP_PROTO,
  849.                       "floop: Charging %ld to %c %s %s",
  850.                       ((inextsecs - isecs) * 1000000
  851.                        + inextmicros - imicros),
  852.                       q->s.bcmd, q->s.zfrom, q->s.zto);
  853.               q->isecs += inextsecs - isecs;
  854.               q->imicros += inextmicros - imicros;
  855.               fcharged = TRUE;
  856.  
  857.               q->fsendfile = FALSE;
  858.  
  859.               if (! (*q->psendfn) (q, qdaemon))
  860.             fret = FALSE;
  861.  
  862.               break;
  863.             }
  864.         }
  865.  
  866.           if (! fret)
  867.         break;
  868.  
  869.           if (! fcharged)
  870.         {
  871.           inextsecs = ixsysdep_process_time (&inextmicros);
  872.           DEBUG_MESSAGE4 (DEBUG_UUCP_PROTO,
  873.                   "floop: Charging %ld to %c %s %s",
  874.                   ((inextsecs - isecs) * 1000000
  875.                    + inextmicros - imicros),
  876.                   q->s.bcmd, q->s.zfrom, q->s.zto);
  877.           q->isecs += inextsecs - isecs;
  878.           q->imicros += inextmicros - imicros;
  879.         }
  880.  
  881.           if (inextsecs - iTchecktime >= CCHECKWAIT)
  882.         {
  883.           if (! fcheck_queue (qdaemon))
  884.             {
  885.               fret = FALSE;
  886.               break;
  887.             }
  888.         }
  889.         }
  890.     }
  891.     }
  892.  
  893.   ulog_user ((const char *) NULL);
  894.  
  895.   (void) (*qdaemon->qproto->pfshutdown) (qdaemon);
  896.  
  897.   if (fret)
  898.     uwindow_acked (qdaemon, TRUE);
  899.   else
  900.     ufailed (qdaemon);
  901.  
  902.   return fret;
  903. }
  904.  
  905. /* This is called by the protocol routines when they have received
  906.    some data.  If pfexit is not NULL, *pfexit should be set to TRUE if
  907.    the protocol receive loop should exit back to the main floop
  908.    routine, above.  It is only important to set *pfexit to TRUE if the
  909.    main loop called the pfwait entry point, so we need never set it to
  910.    TRUE if we just receive data for a file.  This routine never sets
  911.    *pfexit to FALSE.  */
  912.  
  913. boolean 
  914. fgot_data (qdaemon, zfirst, cfirst, zsecond, csecond, ilocal, iremote, ipos,
  915.        fallacked, pfexit)
  916.      struct sdaemon *qdaemon;
  917.      const char *zfirst;
  918.      size_t cfirst;
  919.      const char *zsecond;
  920.      size_t csecond;
  921.      int ilocal;
  922.      int iremote;
  923.      long ipos;
  924.      boolean fallacked;
  925.      boolean *pfexit;
  926. {
  927.   struct stransfer *q;
  928.   int cwrote;
  929.   boolean fret;
  930.   long isecs, imicros;
  931.  
  932.   if (fallacked && qTreceive_ack != NULL)
  933.     uwindow_acked (qdaemon, TRUE);
  934.  
  935.   /* Now we have to decide which transfer structure gets the data.  If
  936.      ilocal is -1, it means that the protocol does not know where to
  937.      route the data.  In that case we route it to the first transfer
  938.      that is waiting for data, or, if none, as a new command.  If
  939.      ilocal is 0, we either select based on the remote channel number
  940.      or we have a new command.  */
  941.   if (ilocal == -1 && qTreceive != NULL)
  942.     q = qTreceive;
  943.   else if (ilocal == 0 && iremote > 0 && aqTremote[iremote] != NULL)
  944.     q = aqTremote[iremote];
  945.   else if (ilocal <= 0)
  946.     {
  947.       const char *znull;
  948.  
  949.       ulog_user ((const char *) NULL);
  950.  
  951.       /* This data is part of a command.  If there is no null
  952.      character in the data, this string will be continued by the
  953.      next packet.  Otherwise this must be the last string in the
  954.      command, and we don't care about what comes after the null
  955.      byte.  */
  956.       znull = (const char *) memchr (zfirst, '\0', cfirst);
  957.       if (znull != NULL)
  958.     fret = ftadd_cmd (qdaemon, zfirst, (size_t) (znull - zfirst),
  959.               iremote, TRUE);
  960.       else
  961.     {
  962.       fret = ftadd_cmd (qdaemon, zfirst, cfirst, iremote, FALSE);
  963.       if (fret && csecond > 0)
  964.         {
  965.           znull = (const char *) memchr (zsecond, '\0', csecond);
  966.           if (znull != NULL)
  967.         fret = ftadd_cmd (qdaemon, zsecond,
  968.                   (size_t) (znull - zsecond), iremote, TRUE);
  969.           else
  970.         fret = ftadd_cmd (qdaemon, zsecond, csecond, iremote, FALSE);
  971.         }
  972.     }
  973.  
  974.       if (pfexit != NULL && (qdaemon->fhangup || qTremote != NULL))
  975.     *pfexit = TRUE;
  976.  
  977.       /* Time spent waiting for a new command is not charged to
  978.          anybody.  */
  979.       if (! fttime (qdaemon, &iTrecsecs, &iTrecmicros))
  980.     fret = FALSE;
  981.  
  982.       return fret;
  983.     }
  984.   else
  985.     {
  986.       /* Get the transfer structure this data is intended for.  */
  987.       q = qtchan (ilocal);
  988.     }
  989.  
  990. #if DEBUG > 0
  991.   if (q == NULL || q->precfn == NULL)
  992.     {
  993.       ulog (LOG_ERROR, "Protocol error: %lu bytes remote %d local %d",
  994.         (unsigned long) (cfirst + csecond),
  995.         iremote, ilocal);
  996.       return FALSE;
  997.     }
  998. #endif
  999.  
  1000.   ulog_user (q->s.zuser);
  1001.  
  1002.   fret = TRUE;
  1003.  
  1004.   if (q->zlog != NULL && ! q->fsendfile)
  1005.     {
  1006.       ulog (LOG_NORMAL, "%s", q->zlog);
  1007.       ubuffree (q->zlog);
  1008.       q->zlog = NULL;
  1009.     }
  1010.  
  1011.   if (cfirst == 0 || q->fcmd || ! q->frecfile || q != qTtiming_rec)
  1012.     {
  1013.       struct stransfer *qcharge;
  1014.  
  1015.       /* Either we are receiving some sort of command, or we are
  1016.          receiving data for a transfer other than the one we are
  1017.          currently timing.  It we are currently timing a transfer,
  1018.          charge any accumulated time to it.  Otherwise, if we
  1019.          currently have something to send, just forget about the
  1020.          accumulated time (when using a bidirectional protocol, it's
  1021.          very difficult to charge this time correctly).  Otherwise,
  1022.          charge it to whatever transfer receives it.  */
  1023.       if (! fttime (qdaemon, &isecs, &imicros))
  1024.     fret = FALSE;
  1025.       if (qTtiming_rec != NULL)
  1026.     qcharge = qTtiming_rec;
  1027.       else if (qTsend != NULL)
  1028.     qcharge = NULL;
  1029.       else
  1030.     qcharge = q;
  1031.       if (qcharge != NULL)
  1032.     {
  1033.       DEBUG_MESSAGE4 (DEBUG_UUCP_PROTO,
  1034.               "fgot_data: Charging %ld to %c %s %s",
  1035.               ((isecs - iTrecsecs) * 1000000
  1036.                + imicros - iTrecmicros),
  1037.               qcharge->s.bcmd, qcharge->s.zfrom,
  1038.               qcharge->s.zto);
  1039.       qcharge->isecs += isecs - iTrecsecs;
  1040.       qcharge->imicros += imicros - iTrecmicros;
  1041.     }
  1042.       iTrecsecs = isecs;
  1043.       iTrecmicros = imicros;
  1044.  
  1045.       /* If we received file data, start timing the new transfer.  */
  1046.       if (cfirst == 0 || q->fcmd || ! q->frecfile)
  1047.     qTtiming_rec = NULL;
  1048.       else
  1049.     qTtiming_rec = q;
  1050.     }
  1051.  
  1052.   /* If we're receiving a command, then accumulate it up to the null
  1053.      byte.  */
  1054.   if (q->fcmd)
  1055.     {
  1056.       const char *znull;
  1057.  
  1058.       znull = NULL;
  1059.       while (cfirst > 0)
  1060.     {
  1061.       size_t cnew;
  1062.       char *znew;
  1063.  
  1064.       znull = (const char *) memchr (zfirst, '\0', cfirst);
  1065.       if (znull != NULL)
  1066.         cnew = znull - zfirst;
  1067.       else
  1068.         cnew = cfirst;
  1069.       znew = zbufalc (q->ccmd + cnew + 1);
  1070.       if (q->ccmd > 0)
  1071.         memcpy (znew, q->zcmd, q->ccmd);
  1072.       memcpy (znew + q->ccmd, zfirst, cnew);
  1073.       znew[q->ccmd + cnew] = '\0';
  1074.       ubuffree (q->zcmd);
  1075.       q->zcmd = znew;
  1076.       q->ccmd += cnew;
  1077.  
  1078.       if (znull != NULL)
  1079.         break;
  1080.  
  1081.       zfirst = zsecond;
  1082.       cfirst = csecond;
  1083.       csecond = 0;
  1084.     }
  1085.  
  1086.       if (znull != NULL)
  1087.     {
  1088.       char *zcmd;
  1089.       size_t ccmd;
  1090.  
  1091.       zcmd = q->zcmd;
  1092.       ccmd = q->ccmd;
  1093.       q->fcmd = FALSE;
  1094.       q->zcmd = NULL;
  1095.       q->ccmd = 0;
  1096.       if (! (*q->precfn) (q, qdaemon, zcmd, ccmd + 1))
  1097.         fret = FALSE;
  1098.       ubuffree (zcmd);
  1099.     }
  1100.  
  1101.       if (pfexit != NULL
  1102.       && (qdaemon->fhangup
  1103.           || qdaemon->fmaster
  1104.           || qTsend != NULL))
  1105.     *pfexit = TRUE;
  1106.     }
  1107.   else if (! q->frecfile || cfirst == 0)
  1108.     {
  1109.       /* We're either not receiving a file or the file transfer is
  1110.      complete.  */
  1111.       q->frecfile = FALSE;
  1112.       if (! (*q->precfn) (q, qdaemon, zfirst, cfirst))
  1113.     fret = FALSE;
  1114.       if (fret && csecond > 0)
  1115.     return fgot_data (qdaemon, zsecond, csecond,
  1116.               (const char *) NULL, (size_t) 0,
  1117.               ilocal, iremote, ipos + (long) cfirst,
  1118.               FALSE, pfexit);
  1119.       if (pfexit != NULL
  1120.       && (qdaemon->fhangup
  1121.           || qdaemon->fmaster
  1122.           || qTsend != NULL))
  1123.     *pfexit = TRUE;
  1124.     }
  1125.   else
  1126.     {
  1127.       if (ipos != -1 && ipos != q->ipos)
  1128.     {
  1129.       DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
  1130.               "fgot_data: Seeking to %ld", ipos);
  1131.       if (! ffileseek (q->e, ipos))
  1132.         {
  1133.           ulog (LOG_ERROR, "seek: %s", strerror (errno));
  1134.           fret = FALSE;
  1135.         }
  1136.       q->ipos = ipos;
  1137.     }
  1138.  
  1139.       if (fret)
  1140.     {
  1141.       while (cfirst > 0)
  1142.         {
  1143.           cwrote = cfilewrite (q->e, (char *) zfirst, cfirst);
  1144.           if (cwrote == cfirst)
  1145.         {
  1146. #if FREE_SPACE_DELTA > 0
  1147.           long cfree_space;
  1148.  
  1149.           /* Check that there is still enough space on the
  1150.              disk.  If there isn't, we drop the connection,
  1151.              because we have no way to abort a file transfer
  1152.              in progress.  */
  1153.           cfree_space = qdaemon->qsys->uuconf_cfree_space;
  1154.           if (cfree_space > 0
  1155.               && ((q->cbytes / FREE_SPACE_DELTA)
  1156.               != (q->cbytes + cfirst) / FREE_SPACE_DELTA)
  1157.               && ! frec_check_free (q, cfree_space))
  1158.             {
  1159.               fret = FALSE;
  1160.               break;
  1161.             }
  1162. #endif
  1163.           q->cbytes += cfirst;
  1164.           q->ipos += cfirst;
  1165.         }
  1166.           else
  1167.         {
  1168.           if (ffileioerror (q->e, cwrote))
  1169.             ulog (LOG_ERROR, "write: %s", strerror (errno));
  1170.           else
  1171.             ulog (LOG_ERROR,
  1172.               "Wrote %d to file when trying to write %lu",
  1173.               cwrote, (unsigned long) cfirst);
  1174.  
  1175.           /* Any write error is almost certainly a temporary
  1176.              condition, or else UUCP would not be functioning
  1177.              at all.  If we continue to accept the file, we
  1178.              will wind up rejecting it at the end (what else
  1179.              could we do?)  and the remote system will throw
  1180.              away the request.  We're better off just dropping
  1181.              the connection, which is what happens when we
  1182.              return FALSE, and trying again later.  */
  1183.           fret = FALSE;
  1184.           break;
  1185.         }
  1186.  
  1187.           zfirst = zsecond;
  1188.           cfirst = csecond;
  1189.           csecond = 0;
  1190.         }
  1191.     }
  1192.  
  1193.       if (pfexit != NULL && qdaemon->fhangup)
  1194.     *pfexit = TRUE;
  1195.     }
  1196.  
  1197.   return fret;
  1198. }
  1199.  
  1200. /* Accumulate a string into a command.  If the command is complete,
  1201.    start up a new transfer.  */
  1202.  
  1203. static boolean
  1204. ftadd_cmd (qdaemon, z, clen, iremote, flast)
  1205.      struct sdaemon *qdaemon;
  1206.      const char *z;
  1207.      size_t clen;
  1208.      int iremote;
  1209.      boolean flast;
  1210. {
  1211.   static char *zbuf;
  1212.   static size_t cbuf;
  1213.   size_t cneed;
  1214.   struct scmd s;
  1215.  
  1216.   cneed = cTcmdlen + clen + 1;
  1217.   if (cneed > cbuf)
  1218.     {
  1219.       zbuf = (char *) xrealloc ((pointer) zbuf, cneed);
  1220.       cbuf = cneed;
  1221.     }
  1222.  
  1223.   memcpy (zbuf + cTcmdlen, z, clen);
  1224.   zbuf[cTcmdlen + clen] = '\0';
  1225.  
  1226.   if (! flast)
  1227.     {
  1228.       cTcmdlen += clen;
  1229.       return TRUE;
  1230.     }
  1231.  
  1232.   /* Don't save this string for next time.  */
  1233.   cTcmdlen = 0;
  1234.  
  1235.   DEBUG_MESSAGE1 (DEBUG_UUCP_PROTO,
  1236.           "ftadd_cmd: Got command \"%s\"", zbuf);
  1237.  
  1238.   if (! fparse_cmd (zbuf, &s)
  1239.       || s.bcmd == 'P')
  1240.     {
  1241.       ulog (LOG_ERROR, "Received garbled command \"%s\"", zbuf);
  1242.       return TRUE;
  1243.     }
  1244.  
  1245.   /* Some systems seem to sometimes send garbage at the end of the
  1246.      command.  Avoid interpreting it as a size if sizes are not
  1247.      supported.  */
  1248.   if ((qdaemon->ifeatures & FEATURE_SIZES) == 0)
  1249.     s.cbytes = -1;
  1250.  
  1251.   if (s.bcmd != 'H' && s.bcmd != 'Y' && s.bcmd != 'N')
  1252.     ulog_user (s.zuser);
  1253.   else
  1254.     ulog_user ((const char *) NULL);
  1255.  
  1256.   switch (s.bcmd)
  1257.     {
  1258.     case 'S':
  1259.     case 'E':
  1260.       return fremote_send_file_init (qdaemon, &s, iremote);
  1261.     case 'R':
  1262.       return fremote_rec_file_init (qdaemon, &s, iremote);
  1263.     case 'X':
  1264.       return fremote_xcmd_init (qdaemon, &s, iremote);
  1265.     case 'H':
  1266.       /* This is a remote request for a hangup.  We close the log
  1267.      files so that they may be moved at this point.  */
  1268.       ulog_close ();
  1269.       ustats_close ();
  1270.       {
  1271.     struct stransfer *q;
  1272.  
  1273.     q = qtransalc ((struct scmd *) NULL);
  1274.     q->psendfn = fremote_hangup_reply;
  1275.     q->iremote = iremote;
  1276.     q->s.bcmd = 'H';
  1277.     return fqueue_remote (qdaemon, q);
  1278.       }
  1279.     case 'N':
  1280.       /* This means a hangup request is being denied; we just ignore
  1281.      this and wait for further commands.  */
  1282.       return TRUE;
  1283.     case 'Y':
  1284.       /* This is a remote confirmation of a hangup.  We reconfirm.  */
  1285.       if (qdaemon->fhangup)
  1286.     return TRUE;
  1287. #if DEBUG > 0
  1288.       if (qdaemon->fmaster)
  1289.     ulog (LOG_ERROR, "Got hangup reply as master");
  1290. #endif
  1291.       /* Don't check errors rigorously here, since the other side
  1292.      might jump the gun and hang up.  The fLog_sighup variable
  1293.      will get set TRUE again when the port is closed.  */
  1294.       fLog_sighup = FALSE;
  1295.       (void) (*qdaemon->qproto->pfsendcmd) (qdaemon, "HY", 0, iremote);
  1296.       qdaemon->fhangup = TRUE;
  1297.       return TRUE;
  1298. #if DEBUG > 0
  1299.     default:
  1300.       ulog (LOG_FATAL, "ftadd_cmd: Can't happen");
  1301.       return FALSE;
  1302. #endif
  1303.     }
  1304. }
  1305.  
  1306. /* The remote system is requesting a hang up.  If we have something to
  1307.    do, send an HN.  Otherwise send two HY commands (the other side is
  1308.    presumed to send an HY command between the first and second, but we
  1309.    don't bother to wait for it) and hang up.  */
  1310.  
  1311. static boolean
  1312. fremote_hangup_reply (qtrans, qdaemon)
  1313.      struct stransfer *qtrans;
  1314.      struct sdaemon *qdaemon;
  1315. {
  1316.   boolean fret;
  1317.  
  1318.   utransfree (qtrans);
  1319.  
  1320.   if (qTremote == NULL
  1321.       && qTlocal == NULL
  1322.       && qTsend == NULL
  1323.       && qTreceive == NULL)
  1324.     {
  1325.       if (! fqueue (qdaemon, (boolean *) NULL))
  1326.     return FALSE;
  1327.  
  1328.       if (qTlocal == NULL)
  1329.     {
  1330.       DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO, "fremote_hangup_reply: No work");
  1331.       fret = ((*qdaemon->qproto->pfsendcmd) (qdaemon, "HY", 0, 0)
  1332.           && (*qdaemon->qproto->pfsendcmd) (qdaemon, "HY", 0, 0));
  1333.       qdaemon->fhangup = TRUE;
  1334.       return fret;
  1335.     }
  1336.     }
  1337.  
  1338.   DEBUG_MESSAGE0 (DEBUG_UUCP_PROTO, "fremote_hangup_reply: Found work");
  1339.   fret = (*qdaemon->qproto->pfsendcmd) (qdaemon, "HN", 0, 0);
  1340.   qdaemon->fmaster = TRUE;
  1341.   return fret;
  1342. }
  1343.  
  1344. /* As described in system.h, we need to keep track of which files have
  1345.    been successfully received for which we do not know that the other
  1346.    system has received our acknowledgement.  This routine is called to
  1347.    keep a list of such files.  */
  1348.  
  1349. static struct sreceive_ack *qTfree_receive_ack;
  1350.  
  1351. void
  1352. usent_receive_ack (qdaemon, qtrans)
  1353.      struct sdaemon *qdaemon;
  1354.      struct stransfer *qtrans;
  1355. {
  1356.   struct sreceive_ack *q;
  1357.  
  1358.   if (qTfree_receive_ack == NULL)
  1359.     q = (struct sreceive_ack *) xmalloc (sizeof (struct sreceive_ack));
  1360.   else
  1361.     {
  1362.       q = qTfree_receive_ack;
  1363.       qTfree_receive_ack = q->qnext;
  1364.     }
  1365.  
  1366.   q->qnext = qTreceive_ack;
  1367.   q->zto = zbufcpy (qtrans->s.zto);
  1368.   q->ztemp = zbufcpy (qtrans->s.ztemp);
  1369.   q->fmarked = FALSE;
  1370.  
  1371.   qTreceive_ack = q;
  1372. }
  1373.  
  1374. /* This routine is called by the protocol code when either all
  1375.    outstanding data has been acknowledged or one complete window has
  1376.    passed.  It may be called directly by the protocol, or it may be
  1377.    called via fgot_data.  If one complete window has passed, then all
  1378.    unmarked receives are marked, and we know that all marked ones have
  1379.    been acked.  */
  1380.  
  1381. void
  1382. uwindow_acked (qdaemon, fallacked)
  1383.      struct sdaemon *qdaemon;
  1384.      boolean fallacked;
  1385. {
  1386.   register struct sreceive_ack **pq;
  1387.  
  1388.   pq = &qTreceive_ack;
  1389.   while (*pq != NULL)
  1390.     {
  1391.       if (fallacked || (*pq)->fmarked)
  1392.     {
  1393.       struct sreceive_ack *q;
  1394.  
  1395.       q = *pq;
  1396.       (void) fsysdep_forget_reception (qdaemon->qsys, q->zto,
  1397.                        q->ztemp);
  1398.       ubuffree (q->zto);
  1399.       ubuffree (q->ztemp);
  1400.       *pq = q->qnext;
  1401.       q->qnext = qTfree_receive_ack;
  1402.       qTfree_receive_ack = q;
  1403.     }
  1404.       else
  1405.     {
  1406.       (*pq)->fmarked = TRUE;
  1407.       pq = &(*pq)->qnext;
  1408.     }
  1409.     }
  1410. }
  1411.  
  1412. /* This routine is called when an error occurred and we are crashing
  1413.    out of the connection.  It is used to report statistics on failed
  1414.    transfers to the statistics file, and it also discards useless
  1415.    temporary files for file receptions.  Note that the number of bytes
  1416.    we report as having been sent has little or nothing to do with the
  1417.    number of bytes the remote site actually received.  */
  1418.  
  1419. void
  1420. ufailed (qdaemon)
  1421.      struct sdaemon *qdaemon;
  1422. {
  1423.   register struct stransfer *q;
  1424.  
  1425.   if (qTsend != NULL)
  1426.     {
  1427.       q = qTsend;
  1428.       do
  1429.     {
  1430.       if ((q->fsendfile || q->frecfile)
  1431.           && q->cbytes > 0)
  1432.         {
  1433.           ustats (FALSE, q->s.zuser, qdaemon->qsys->uuconf_zname,
  1434.               q->fsendfile, q->cbytes, q->isecs, q->imicros,
  1435.               qdaemon->fcaller);
  1436.           if (q->fsendfile)
  1437.         qdaemon->csent += q->cbytes;
  1438.           else
  1439.         qdaemon->creceived += q->cbytes;
  1440.         }
  1441.       if (q->frecfile)
  1442.         (void) frec_discard_temp (qdaemon, q);
  1443.       q = q->qnext;
  1444.     }
  1445.       while (q != qTsend);
  1446.     }
  1447.  
  1448.   if (qTreceive != NULL)
  1449.     {
  1450.       q = qTreceive;
  1451.       do
  1452.     {
  1453.       if ((q->fsendfile || q->frecfile)
  1454.           && q->cbytes > 0)
  1455.         {
  1456.           ustats (FALSE, q->s.zuser, qdaemon->qsys->uuconf_zname,
  1457.               q->fsendfile, q->cbytes, q->isecs, q->imicros,
  1458.               qdaemon->fcaller);
  1459.           if (q->fsendfile)
  1460.         qdaemon->csent += q->cbytes;
  1461.           else
  1462.         qdaemon->creceived += q->cbytes;
  1463.         }
  1464.       if (q->frecfile)
  1465.         (void) frec_discard_temp (qdaemon, q);
  1466.       q = q->qnext;
  1467.     }
  1468.       while (q != qTreceive);
  1469.     }
  1470. }
  1471.  
  1472. /* When a local poll file is found, it is entered on the queue like
  1473.    any other job.  When it is pulled off the queue, this function is
  1474.    called.  It just calls fsysdep_did_work, which will remove the poll
  1475.    file.  This ensures that poll files are only removed if the system
  1476.    is actually called.  */
  1477.  
  1478. /*ARGSUSED*/
  1479. static boolean
  1480. flocal_poll_file (qtrans, qdaemon)
  1481.      struct stransfer *qtrans;
  1482.      struct sdaemon *qdaemon;
  1483. {
  1484.   boolean fret;
  1485.  
  1486.   fret = fsysdep_did_work (qtrans->s.pseq);
  1487.   utransfree (qtrans);
  1488.   return fret;
  1489. }
  1490.